﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;//这个是判断是否符合正则表达式的引用
using System.Threading.Tasks;
using System.Windows.Forms;


namespace StuManagementSystem
{
    public partial class frm_Login : Form
    {
        /// <summary>
        /// 导入数据库操作类
        /// </summary>
        DbContext db = new DbContext();
        /// <summary>
        /// 构造方法【1、没有返回值类型
        ///           2、方法名和类名相同 】
        ///     作用：  用于类执行初始化操作，每一个类都有一个构造方法
        ///     无参方法可以省略，由编译器自动创建。
        /// </summary>
        public frm_Login()
        {
            InitializeComponent();
        }

        private void frm_Login_Load(object sender, EventArgs e)
        {

        }


        #region 窗口可拖动
        Point mouseOff;//鼠标移动位置变量
        bool leftFlag;//标签是否为左键

        /// <summary>
        /// 鼠标左键按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }

        /// <summary>
        /// 鼠标左键放开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }

        /// <summary>
        /// 鼠标左键按住移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }
        #endregion

        #region 登录按钮
        /// <summary>
        /// 登录按钮
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        private void btn_Login_Click(object sender, EventArgs e)
        {
            //读取当前窗体的用户名
            var UserName = this.txt_UserName.Text.Trim();
            //读取当前窗体的密码
            var UserPwd = this.txt_UserPwd.Text.Trim();
            //读取当前窗体的登录类型
            var UserType = this.cbo_UserType.Text.Trim();
            //判断是否为空
            if (CheckIsNull(UserName, UserPwd, UserType) == false)
                return; //在程序中单加return，可以中断程序运行

            #region 为管理员时登录
            if (UserType.Equals("管理员"))
            {
                string sql = string.Format(@"select AdminId from Admin where LoginId = '{0}' and LoginPwd = '{1}' and LoginType = '{2}'", UserName, UserPwd, UserType);
                int num = Convert.ToInt32(db.ExecScaler(sql));
                if (num > 0)
                {
                    frm_ManagerIndex frm = new frm_ManagerIndex(num);
                    frm.Show();
                    frm.Text = "MySchool—" + cbo_UserType.Text;
                    frm.Owner = this;
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("登录失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            #endregion

            #region 为学生时登录
            else
            {
                string sql = "";
                //判断是否为数字
                if (Regex.IsMatch(UserName, "^[0-9]*$"))
                    sql = string.Format(@"select StuCode from StuInfo where StuCode = '{0}' and StuPwd = '{1}'", UserName, UserPwd);
                else
                    sql = string.Format(@"select StuCode from StuInfo where StuName = '{0}' and StuPwd = '{1}'", UserName, UserPwd);
                int num = Convert.ToInt32(db.ExecScaler(sql));
                if (num > 0)
                {
                    frm_StuIndex frm = new frm_StuIndex(num);
                    frm.Show();
                    frm.Text = "MySchool—" + cbo_UserType.Text;
                    frm.Owner = this;
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("登录失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            #endregion
            

        }

        #region 检查是否为空的代码
        /// <summary>
        /// 检查是否为空方法
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="UserPwd"></param>
        /// <param name="UserType"></param>
        /// <returns></returns>
        private bool CheckIsNull(string UserName, string UserPwd, string UserType)
        {
            if (string.IsNullOrWhiteSpace(UserName))
            {
                MessageBox.Show("请输入用户名", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txt_UserName.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(UserPwd))
            {
                MessageBox.Show("请输入密码", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txt_UserPwd.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(UserType))
            {
                MessageBox.Show("请输入登录类型", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.cbo_UserType.Focus();
                return false;
            }
            return true;
        }
        #endregion
        
        #endregion

        #region 取消按钮
        /// <summary>
        /// 单击取消按钮
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">事件参数</param>
        private void btn_Pass_Click(object sender, EventArgs e)
        {
            //Button obj = sender as Button; //把sender转换为Button格式

            DialogResult dr = MessageBox.Show("        确认退出？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
                this.Close();//写在启动页面窗口就是退出整个窗口，写在其他窗口内就只是关闭当前窗口。
            //退出程序
            //Application.Exit();//这个就是退出整个程序。
            //this.Hide();//只有隐藏启动也才用Hide();其余窗口关闭均使用this.Close();
        }
        #endregion

        #region 回车登录
        /// <summary>
        /// 回车登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_UserPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.btn_Login_Click(null, null);
            }
        }
        #endregion

        #region 开始动画

        #endregion

        
    }
}
